package bus;

import java.util.ArrayList;

import dao.AdminDao;
import dao.StudentDao;
import dao.SubjectDao;
import dao.SubjectHasStudentDao;
import dao.TeacherDao;
import dao.UserDao;
import dto.AdminDto;
import dto.StudentDto;
import dto.SubjectDto;
import dto.SubjectHasStudentDto;
import dto.TeacherDto;
import dto.UserDto;

public class CreateUserBus {

	public static String createNewStudent(StudentDto student, UserDto user,
			ArrayList<String> subjectNames) {

		String response = "";

		StudentDao studentDao = new StudentDao();
		/* insert new student */
		response = studentDao.insertStudent(student);
		if (response.equals("Inserted successfully!")) {

			UserDao userDao = new UserDao();
			/* insert new user */
			response = userDao.insertUser(user);

			/* update user id for student */
			student.setUserId(user.getUserId());
			studentDao.updateStudent(student);

			SubjectHasStudentDao subjectHasStudentDao = new SubjectHasStudentDao();
			SubjectDao subjectDao = new SubjectDao();
			for (String subjectName : subjectNames) {
				SubjectDto subject = subjectDao.getSubjectByName(subjectName);
				SubjectHasStudentDto subjectHasStudentDto = new SubjectHasStudentDto(
						subject.getSubjectId(), student.getId());
				subjectHasStudentDao
						.insertSubjectHasStudent(subjectHasStudentDto);
			}
		}
		return response;
	}

	public static String createNewTeacher(TeacherDto teacher, UserDto user) {

		String response = "";

		TeacherDao teacherDao = new TeacherDao();
		/* insert new teacher */
		response = teacherDao.insertTeacher(teacher);
		if (response.equals("Inserted successfully!")) {

			UserDao userDao = new UserDao();
			/* insert new user */
			response = userDao.insertUser(user);

			/* update user id for teacher */
			teacher.setUserId(user.getUserId());
			teacherDao.updateTeacher(teacher);
		}
		return response;
	}

	public static String createNewAdmin(AdminDto admin, UserDto user) {

		String response = "";

		AdminDao AdminDao = new AdminDao();
		/* insert new Admin */
		response = AdminDao.insertAdmin(admin);
		if (response.equals("Inserted successfully!")) {

			UserDao userDao = new UserDao();
			/* insert new user */
			response = userDao.insertUser(user);

			/* update user id for Admin */
			admin.setUserId(user.getUserId());
			AdminDao.updateAdmin(admin);
		}
		return response;
	}

}
